/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * About.java
 *
 * Created on Mar 30, 2010, 7:33:29 PM
 */
package project.jpdftools.ui;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import javax.swing.BoundedRangeModel;
import javax.swing.JDialog;
import javax.swing.Timer;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import project.jpdftools.managers.LocalizationManager;
import project.jpdftools.managers.LoggerManager;
import project.jpdftools.managers.other.FileUtil;

/**
 *
 * @author Mido
 */
public class WhatISNewScroll extends JDialog {

    private static boolean is_component_visible = false;
    private static final int SCROLL_AMOUNT = 2; // the amount to scroll
    private BoundedRangeModel barModel;  // Variable to hold the bounded range model
    Timer timer;    // timer to control the movement of the credits

    /**
     * Creates new form About
     */
    public WhatISNewScroll() {
        initComponents();

        // center the component
        project.jpdftools.toolkit.Toolkit.centerWindow(this, this.getHeight(), this.getWidth());
        this.setAlwaysOnTop(true);

        // indicate that the frame is visible
        is_component_visible = true;
        LoggerManager.getLogger().log(Level.INFO, "Frame shown \"{0}\".", "WhatIsNewScroll");

        // set the text inside the text pane to be aligned to the center
        StyledDocument doc = tp_credits.getStyledDocument();
        SimpleAttributeSet center = new SimpleAttributeSet();
        StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
        doc.setParagraphAttributes(0, doc.getLength(), center, false);

        // set the caret to the begining of the textpane
        tp_credits.setCaretPosition(0);

        // extract the BoundedRangeModel from the vertical scroll bar
        barModel = scp_credits.getVerticalScrollBar().getModel();
        final int TIMER_DELAY = 100;

        // create the timer object and add the action listener
        timer = new Timer(TIMER_DELAY, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                timerActionPerformed(e);
            }
        });

        // start the timer
        timer.start();
    }

    /**
     * Action happens when the timer changes.
     *
     * @param e
     */
    private void timerActionPerformed(ActionEvent e) {
        int newModelValue = barModel.getValue();  // Get the model's curent value
        if (newModelValue < barModel.getMaximum() - barModel.getExtent()) {  // if not at max
            newModelValue += SCROLL_AMOUNT; // add something to it
            barModel.setValue(newModelValue);  // and change the model's value
        } else {
            // reset the barModel to the begining of the vertical scrollpane
            barModel.setValue(0);
        }
    }

    public static boolean IsComponentVisible() {
        return is_component_visible;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bt_close = new javax.swing.JButton();
        scp_credits = new javax.swing.JScrollPane();
        tp_credits = new javax.swing.JTextPane();
        cb_pause = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(LocalizationManager.getCurrent_resource().getString("WHAT_IS_NEW_TITLE"));
        setIconImage(Toolkit.getDefaultToolkit().getImage("Data/Icons/Soft Scraps Icons/10.png"));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                WhatISNewScroll.this.windowClosing(evt);
            }
        });

        bt_close.setText(LocalizationManager.getCurrent_resource().getString("CLOSE"));
        bt_close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_closeActionPerformed(evt);
            }
        });

        tp_credits.setContentType("text/html");
        tp_credits.setEditable(false);
        tp_credits.setFont(new java.awt.Font("Times New Roman", 0, 12));
        tp_credits.setText(new FileUtil().readALlDataUsingEncoding(LocalizationManager.getCurrent_resource().getString("WHAT_IS_NEW_FILE_LOCATION"),"UTF8"));
        tp_credits.setOpaque(false);
        scp_credits.setViewportView(tp_credits);

        cb_pause.setText(LocalizationManager.getCurrent_resource().getString("ABOUT_PAUSE_ME"));
        cb_pause.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_pauseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cb_pause)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 371, Short.MAX_VALUE)
                        .addComponent(bt_close))
                    .addComponent(scp_credits, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 561, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scp_credits, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_close)
                    .addComponent(cb_pause))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_closeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_closeActionPerformed
        // close the frame
        this.dispose();
        // indicate that the frame is hidden
        is_component_visible = false;
        LoggerManager.getLogger().log(Level.INFO, "Frame hidden \"{0}\".", "WhatIsNewScroll");
    }//GEN-LAST:event_bt_closeActionPerformed

    private void windowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowClosing
        bt_closeActionPerformed(null);
    }//GEN-LAST:event_windowClosing

    private void cb_pauseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_pauseActionPerformed
        if (cb_pause.isSelected() == true) {
            // pause the movement
            timer.stop();
        } else {
            // continue movement
            timer.restart();
        }
    }//GEN-LAST:event_cb_pauseActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_close;
    private javax.swing.JCheckBox cb_pause;
    private javax.swing.JScrollPane scp_credits;
    private javax.swing.JTextPane tp_credits;
    // End of variables declaration//GEN-END:variables
}
